package com.zyk.scaffold.juc;

import java.util.concurrent.Semaphore;

public class SemaphoreTest {
    public static void main(String[] args) throws InterruptedException {
        Semaphore product = new Semaphore(1);
        Semaphore consumer = new Semaphore(0);

        new Thread(() -> {
            while(Boolean.TRUE){
                try {
                    consumer.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费1号");
                product.release();
            }
        }).start();

        new Thread(() -> {
            while(Boolean.TRUE){
                try {
                    consumer.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费2号");
                product.release();
            }
        }).start();

        new Thread(() -> {
            while(Boolean.TRUE){
                try {
                    product.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("生产者");
                consumer.release();
            }
        }).start();

        Thread.sleep(100000);
    }
}
